/*
 * @Author: ReinerLau lk850593913@gmail.com
 * @Date: 2023-01-16 11:25:46
 * @LastEditors: ReinerLau lk850593913@gmail.com
 * @LastEditTime: 2023-01-17 14:42:15
 * @FilePath: \v2.0.0\cypress\e2e\patroling\patroling_timing.cy.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
describe("定时任务", () => {
  beforeEach(() => {
    const username = "admin";
    const password = "123456";
    cy.login(username, password);

    cy.visit("/patroling/release");

    cy.getByData("patroling-car-switch").find("svg").eq(0).click();

    cy.getByData("patroling-bottom").find("div").eq(1).click();

    cy.wait(3 * 1000);

    cy.intercept("POST", "/api/vehicle-mission-timer/v1/create", {
      statusCode: 200,
      body: { code: 200 },
    }).as("createTimingMission");
  });

  context("定时任务请求参数", () => {
    it("每天", () => {
      createTimingMission("每天");

      cy.contains("下发时间").siblings().find("input").click();

      cy.get(".el-picker-panel__footer").contains("确定").click();

      cy.get("[aria-label='新建定时任务']").contains("确定").click();

      cy.wait("@createTimingMission").should(({ request }) => {
        const { body } = request;
        expect(body.loopConditions).to.equal("1");
        expect(body.conditions).to.equal("1,2,3,4,5,6,7");
        expect(body.changeMission).to.equal(false);
      });
    });

    it("每周", () => {
      createTimingMission("每周");

      cy.contains("循环时间").siblings().find("label").as("conditions");
      cy.get("@conditions").eq(0).click();
      cy.get("@conditions").eq(2).click();
      cy.get("@conditions").eq(4).click();

      cy.contains("下发时间").siblings().find("input").click();

      cy.get(".el-picker-panel__footer").contains("确定").click();

      cy.get("[aria-label='新建定时任务']").contains("确定").click();

      cy.wait("@createTimingMission").should(({ request }) => {
        const { body } = request;
        expect(body.loopConditions).to.equal("2");
        expect(body.conditions).to.equal("1,3,5");
        expect(body.changeMission).to.equal(false);
      });
    });

    it("单次", () => {
      createTimingMission("单次");

      cy.contains("下发时间").siblings().find("input").click();

      cy.get(".el-picker-panel__footer").contains("确定").click();

      cy.get("[aria-label='新建定时任务']").contains("确定").click();

      cy.wait("@createTimingMission").should(({ request }) => {
        const { body } = request;
        expect(body.loopConditions).to.equal("3");
        expect(body.conditions).to.equal("");
        expect(body.changeMission).to.equal(false);
      });
    });
  });

  it("循环条件切换正确", () => {
    createTimingMission("每天");

    cy.contains("循环时间").siblings().find("label").as("conditions");
    cy.contains("循环条件").siblings().find("input").as("loopConditions");

    cy.get("@conditions").should("have.class", "is-disabled");
    cy.get("@conditions").should("have.class", "is-checked");

    cy.get("@loopConditions").click();
    cy.contains("每周").click();
    cy.get("@conditions").should("not.have.class", "is-disabled");
    cy.get("@conditions").should("not.have.class", "is-checked");

    cy.get("@loopConditions").click();
    cy.contains("单次").click();
    cy.get("@conditions").should("have.class", "is-disabled");
    cy.get("@conditions").should("not.have.class", "is-checked");
  });

  it("搜索定时任务", () => {
    cy.intercept("GET", "/api/vehicle-mission-timer/v1/list", {
      statusCode: 200,
      body: { code: 200, data: { list: [{ id: "1" }] } },
    });

    cy.contains("定时任务").click();

    cy.contains("搜索定时任务").click();

    cy.get(".el-table__row").should("have.length", 1);
  });

  it.only("删除定时任务", () => {
    cy.intercept("GET", "/api/vehicle-mission-timer/v1/list", {
      statusCode: 200,
      body: { code: 200, data: { list: [{ id: "1" }] } },
    });

    cy.intercept("DELETE", "/api/vehicle-mission-timer/v1/delete/*", {
      statusCode: 200,
      body: { code: 200 },
    }).as("deleteTimingMission");

    cy.contains("定时任务").click();

    cy.contains("搜索定时任务").click();

    cy.getByData("delete-button").click();

    cy.intercept("GET", "/api/vehicle-mission-timer/v1/list", {
      statusCode: 200,
      body: { code: 200, data: { list: [] } },
    }).as("getTimingMission");

    cy.get(".el-message-box").find("button").contains("确定").click();

    cy.wait("@deleteTimingMission");
    cy.wait("@getTimingMission");

    cy.get(".el-table__row").should("have.length", 0);
  });
});

function createTemplate() {
  const templateName = "test";

  cy.contains("模板").click();

  cy.contains("新建模板").click();

  cy.contains("任务模板名称").siblings().find("input").type(templateName);

  cy.contains("任务描述").siblings().find("input").type(templateName);

  cy.get("[aria-label='新建模板']").find("button").contains("确定").click();

  cy.getByData("patroling-map").as("map");

  cy.get("@map").click(500, 500);

  cy.get("@map").dblclick(700, 500);

  cy.contains("模板").click();

  cy.contains("保存模板").click();
}

function selectTemplate() {
  cy.contains("模板").click();

  cy.contains("搜索模板").click();

  cy.getByData("template-list").scrollTo("bottom");

  cy.getByData("template-item").eq(-1).click();

  cy.contains("确定").click();
}

function createTimingMission(conditions) {
  //   createTemplate();

  selectTemplate();

  cy.contains("定时任务").click();

  cy.contains("新建定时任务").click();

  cy.contains("循环条件").siblings().find("input").click();

  cy.contains(conditions).click();
}
